﻿using UnityEditor;
using UnityEditor.MemoryProfiler;
using UnityEngine;

namespace PerfAssist.MemoryProfilerAdvanced.Editor.PAContrib.TrackerModes
{
    public class TrackerMode_Editor : TrackerMode_Base
    {
        private Rect _last;

        public override void OnAppStarted()
        {
            TrackerModeUtil.Connect(MemConst.LocalhostIP);
        }

        protected override void Do_GUI()
        {
            if (GUILayout.Button("Take Snapshot", EditorStyles.toolbarButton, GUILayout.Width(120), GUILayout.Height(20)))
                MemorySnapshot.RequestNewSnapshot();

            if (Event.current.type == EventType.Repaint)
                _last = GUILayoutUtility.GetLastRect();

            GUILayout.Space(DrawIndicesGrid(_last.xMax + 20, _last.y));
        }

        public override bool SaveSessionInfo(PackedMemorySnapshot packed)
        {
            var sessionName = _sessionTimeStr + TrackerModeConsts.EditorTag;
            return TrackerModeUtil.SaveSnapshotFiles(sessionName, _selected.ToString(), packed);
        }

        public override bool SaveSessionJson(CrawledMemorySnapshot Unpacked)
        {
            var sessionName = _sessionTimeStr + TrackerModeConsts.EditorTag;
            return TrackerModeUtil.SaveSnapshotJson(sessionName, _selected + ".json", Unpacked);
        }
    }
}